/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-10 10:57:39
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-10 11:46:54
 * @FilePath: /webServer/src/service/department/department.service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Provide } from '@midwayjs/core';
import { ServicesImpl } from '../impl';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { TreeRepository } from 'typeorm';
import { Department } from '../../model/department/department';
import { DepartmentSchema } from '../../schema';

@Provide()
export class DepartmentService extends ServicesImpl {
  /**
   * 覆盖实体
   */
  @InjectEntityModel(Department)
  services: TreeRepository<Department>;

  /**
   * 重写关系
   */
  relations: Array<string>;

  constructor() {
    super();
    this.relations = ['parent', 'children'];
  }

  async trees() {
    return await this.services.findTrees({ relations: this.relations });
  }

  async save(params: DepartmentSchema) {
    const dept = await this.services.findOneBy({ id: params.parent.id });
    Object.assign(params, { parent: dept });
    return await this.services.save(params);
  }
}
